package com.zk.yygh.hosp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zk.yygh.hosp.exception.HospitalException;
import com.zk.yygh.hosp.mapper.HospitalSetMapper;
import com.zk.yygh.hosp.result.ResultCodeEnum;
import com.zk.yygh.hosp.service.HospitalSetService;
import com.zk.yygh.model.hosp.HospitalSet;
import com.zk.yygh.util.MD5;
import com.zk.yygh.vo.order.SignInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: 张凯
 * @date: 2022/5/11 - 18:51
 */
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {

    @Autowired
    private HospitalSetMapper hospitalSetMapper;

    @Override
    public String getSignKey(String hospCode) {
        System.out.println("----hospcode: "+hospCode);
        HospitalSet hospitalSet = this.getByHospcode(hospCode);
        if (hospitalSet==null){
            //查无此医院，医院未开通
            throw new HospitalException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        if (hospitalSet.getStatus().intValue()==0){
            //医院被锁定，暂时不能用
            throw new HospitalException(ResultCodeEnum.HOSPITAL_LOCK);
        }
        return hospitalSet.getSignKey();
    }

    @Override
    public SignInfoVo getSignInfoVo(String hoscode) {
        //查询出医院
        HospitalSet hospitalSet = baseMapper.selectOne(new QueryWrapper<HospitalSet>().eq("hoscode", hoscode));
        if (hospitalSet==null){
            throw new HospitalException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        //创建引用签名对象
        SignInfoVo signInfoVo = new SignInfoVo();
        signInfoVo.setApiUrl(hospitalSet.getApiUrl());
        signInfoVo.setSignKey(MD5.encrypt(hospitalSet.getSignKey()));
        return signInfoVo;
    }

    private HospitalSet getByHospcode(String hospCode) {
        return hospitalSetMapper.selectOne(new QueryWrapper<HospitalSet>().eq("hoscode",hospCode));
    }
}
